<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;

use buwang\base\AdminBaseController;
use buwang\traits\CrudControllerTrait;

class AppGeneratorController extends AdminBaseController
{
    use CrudControllerTrait;

    protected $model = null;//模型实例

    protected function initialize()
    {
        parent::initialize();
    }
    /**
     * 生成初始化应用
     */
    public function initAppGenerator()
    {
        $params = request()->post();
        if (!isset($params['types']) || !$params['types']) {
            return $this->error('应用类型不能为空');
        }
        if (!isset($params['types']) || !$params['types']) {
            return $this->error('应用类型不能为空');
        }
        if (!isset($params['title']) || !$params['title']) {
            return $this->error('应用名称不能为空');
        }
        if (!isset($params['describe']) || !$params['describe']) {
            return $this->error('应用描述不能为空');
        }
        if (!isset($params['app_dir']) || !$params['app_dir']) {
            return $this->error('应用目录不能为空');
        }

        // 判断字符串只能是字母和数字
        if(!ctype_alnum($params['app_dir'])){
            return $this->error('应用目录只能是字母和数字');
        }

        $params['types'] = ['mini_program','app'];

        //应用类型必须是数组格式
        if (!is_array($params['types'])) {
            return $this->error('应用类型必须是数组格式');
        }

        //应用类型
        $types = $params['types'];
        //转换成逗号拼接
        $data['types'] = implode(',', $types);
        //应用版本号
        $data['version'] = $params['version']??'1.0.1';
        //应用名称
        $data['title'] = $params['title'];
        //应用描述
        $data['describe'] = $params['describe'];
        //应用目录-应用的数据库表前缀如 bw_bwmall_config,中间的bwmall就是此应用表的标识，要保证全平台唯一
        $data['app_dir'] = $params['app_dir'];

        //平台管理0关闭1启用
        $data['is_manage'] = $params['is_manage']??0;
        //接入方式1平台、0独立
        $data['is_openapp'] = $params['is_openapp']??0;
        //微信支付  0、1
        $data['is_wechat_pay'] = $params['is_wechat_pay']??0;
        //支付宝支付  0、1
        $data['is_alipay_pay'] = $params['is_alipay_pay']??0;

        $miniapp = new \app\common\model\Miniapp();
        $data = $miniapp->initAppGenerator($data);

        return $this->success('successful',$data);
    }




}
